package org.linlinjava.litemall.admin.service;


import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallMoneyLogService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Service
public class AsyncService {

    @Autowired
    private LitemallUserService userService;
@Autowired
private LitemallMoneyLogService  litemallMoneyLogService;


    @Async
    public Future<String>  getfwsmoney(int  lv,int userid,double  mytxj){
        LitemallUser  litemallUser= userService.findById(userid);
            double myfst = litemallUser.getFSPT();
            double myfsp = litemallUser.getFSP();
            double mysun = litemallUser.getSUN();
            if (myfst < 0 && myfsp < 0) {
                /**
                 ** 记录完全没有钱的 亏损
                 */
                userService.MoneyProup(userid, mytxj, 5);
                litemallMoneyLogService.insertMoney(0, mytxj, "服务商完全没有FST与FSP产生的亏损", userid, "亏损", 5, 0.00, 0);
                return new AsyncResult<>("执行成功");
            }
            int fstzfsp = 0;
            double addfst = 0.00;
            double addfsp = 0.00;
            double addsun = 0.00;
            double addks = 0.00;

            if (myfst - mytxj >= 0) {

                addfst = addfst + mytxj;
                addsun = addsun + mytxj;

                //当前有多余的fst  可以进入fst转 fsp
                if (myfst - mytxj > 0) {
                    fstzfsp = 1;
                    myfst = myfst - mytxj;
                }
            } else {
                //不够扣  , 如果有 fstp  ,先扣到0
                if (myfst > 0) {
                    addfst = addfst + myfst;
                    addsun = addsun + myfst;
                    //总数 -扣掉的FSPT
                    mytxj = mytxj - myfst;
                    //没有钱
                    if (myfsp == 0) {
                        addks = addks + Math.abs(mytxj);
                        return new AsyncResult<>("执行成功");
                    }
                    //判断 fsp 是否 够扣
                    if (myfsp - mytxj < 0) {
                         //不够扣，有多少 扣多少
                        mytxj = myfsp;
                    }
                    addfsp = addfsp - myfsp;
                    addsun = addsun + myfsp;
                } else {
                    //不够扣  , 且没有fst
                    if (myfsp == 0) {
                        addks = addks + mytxj;
                        return new AsyncResult<>("执行成功");
                    }
                    //判断 fsp 是否 够扣
                    if (myfsp - mytxj < 0) {
                  //不够扣，有多少 扣多少
                        mytxj = myfsp;
                    }
                    addfsp = addfsp - mytxj;
                }
            }
            /**
             * 开始将 fst 转 fsp
             */
            if (fstzfsp == 1) {
                if (myfst - mytxj >= 0) {
                    addfsp = addfsp + mytxj;
                    addfst = addfst + mytxj;
                } else {
                    addfsp = addfsp + myfst;
                    addfst = addfst + myfst;
                }
            }
            if (addfst != 0.00) {
                userService.MoneyProdown(userid, addfst, 1);
                litemallMoneyLogService.insertMoney(addfst, 0, "服务商获得提现奖失去的所有FST", userid, "FST", 6, 0.00, 0);
            }
            if (addsun != 0.00) {
                userService.MoneyProup(userid, addsun, 2);
                litemallMoneyLogService.insertMoney(0, addsun, "服务商获得提现奖获得的所有SUN", userid, "SUN", 6, 0.00, 0);
            }
            if (addfsp != 0.00) {
                userService.MoneyProup(userid, addfsp, 0);
                litemallMoneyLogService.insertMoney(addfsp > 0 ? 0 : addfsp, addfsp > 0 ? addfsp : 0, "服务商获得提现奖获得的所有FSP", userid, "FSP", 6, 0.00, 0);
            }
            if (addks != 0.00) {
                userService.MoneyProup(userid, addks, 5);
                litemallMoneyLogService.insertMoney(0, addks, "服务商提现奖产生的所有亏损", userid, "亏损", 6, 0.00, 0);
            }
        return new AsyncResult<>("执行成功");
    }

}
